/*
 * @Author: 周艳凯 484894717@qq.com
 * @Date: 2025-03-17 17:04:06
 * @LastEditors: 周艳凯 484894717@qq.com
 * @LastEditTime: 2025-04-10 14:59:13
 * @FilePath: \BusStopBoard.Webd:\360MoveData\Users\75041\Desktop\uniapp框架\my-project\src\stores\index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
// 导入 vue
import Vue from 'vue'
// 导入 vuex
import Vuex from 'vuex'

import createPersistedState from 'vuex-persistedstate'
// import { getitemlistbydevsn } from '@/api/common/user/user';
// vuex也是vue的插件, 需要use一下, 进行插件的安装初始化
Vue.use(Vuex)


// 创建仓库 store
const store = new Vuex.Store({
    state: {
        // 是否第一次进入app
        isFirst: true,
        // webscok链接状态
        isWebscok:false,
        apiUrl: process.env.VUE_APP_API_URL,
        // 安卓id
        androidId: null,
        // 轮播图
        swiperList: [],
        // 推过来的报站数据
        receiveUserName: {},
        textArray: [],
        lineNode: [],
        textData: [],
        StationConfig: {
            // 图片轮播停留秒数
            intervalSecond: 15000,
            promotionSlogan: [
                '低碳出行，从我做起',
                '主动让座，关爱他人',
                '文明乘车，先下后上',
                '文明礼行，文明交流',
                '爱护环境，保持整洁',
                '遵守规定，规范乘车',
                '选择公交，绿色出行',
                '车内安静，文明交流',
            ],
            // 竹节点向上翻滚秒数
            stopSecond: 16000,
            // 界面多久没有操作恢复原状
            restitution: 3000,
        },
        // 推送过来的文本
        PushText: new Array(20).fill(null),
    },
    getters: {
        getswiperList: state => {
            return state.swiperList
        },
        androidId: state => state.androidId,
        isWebscok: state => state.isWebscok,
        gettextArray: state => {
            return state.textArray
        },
        getreceiveUserName: state => {
            return state.receiveUserName
        },
        getlineNode: state => {
            return state.lineNode
        },
        getConfig: state => {
            return state.StationConfig
        },
        getPushText: state => {
            return state.PushText
        },
    },
    mutations: {
        // 改变api地址
        setSocket(state, flog) {
            state.isWebscok = flog
        },
        increment(state) {
            state.androidId++
        },
        changeStationConfig(state, list) {
            state.StationConfig = list
        },
        changetextArray(state, list) {
            state.textArray = list
        },
        changeSwiper(state, list) {
            state.swiperList = list
        },
        changetextData(state, list) {
            state.textData = list
        },

        changelineNode(state, list) {
            state.lineNode = list
        },

        SET_ANDROID_ID(state, id) {
            state.androidId = id;
        },
        SET_receiveUserName(state, id) {
            state.receiveUserName = id;
        },
        // 改变api地址
        SET_api(state, id) {
            state.apiUrl = id;
            state.isFirst = false;
        },
        // 去改变文本
        changePushText(state, params) {
            if (params.lineIds == 'Clear') {
                state.PushText = new Array(20).fill(null)
                return
            }
            const rawIndex = Number(params.indexs); // 也可以将字符串转换为数字
            if (isNaN(rawIndex)) {
                throw new Error("Invalid index format");
            }
            const index = rawIndex - 1; // 调整为从 0 开始的索引
            // 检查索引是否在数组范围内
            if (index < 0 || index >= state.PushText.length) {
                throw new Error("Index out of bounds");
            }
            console.log(state.PushText, index, params.message, '我很无语啊老弟')
            Vue.set(state.PushText, index, params.message)
        }
    },
    plugins: [createPersistedState({
        storage: {
            getItem: (key) => uni.getStorageSync(key),
            setItem: (key, value) => uni.setStorageSync(key, value),
            removeItem: key => uni.removeStorageSync(key)
        },
        reducer: state => ({
            isFirst: state.isFirst,
            apiUrl:state.apiUrl,
            // 安卓id
            androidId: state.androidId,
            // 轮播图
            swiperList: state.swiperList,
            textArray: state.textArray,
            lineNode: state.lineNode,
            textData: state.textData,
            StationConfig: state.StationConfig,
            // 推送过来的文本
            PushText: state.PushText,
        })
    })]
})


// 导出仓库
export default store